package Tests;

import Common.DataBase;
import Common.TestBase;

import Pages.ArticlePage;
import Pages.ForwardThisPage;

import Common.Application;
import org.openqa.selenium.By;
import org.testng.annotations.*;
import java.lang.reflect.Method;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertEquals;

/**
 * Forward this page test
 */

public class ForwardThisPageTest extends TestBase {

    protected static DataBase data = new DataBase();
    protected static ForwardThisPage forward = new ForwardThisPage();
    protected static ArticlePage articlePage = new ArticlePage();

    @BeforeMethod (groups = {"done","nonstable"})
    public void beforeMethod(final Method method) {
        hb.openBaseUrl();}

    //positive message sending test
    @Test (groups = {"done"})
    public void messageSending(){
        application.goToArticle3();
        application.goToForwardThisPage();
        application.messageSending(data.ValidEmailHN());
        assertTrue(hb.isElementPresent(By.xpath(articlePage.alertRolling())));
    }
    //negative "Your email" field test
    @Test (groups = {"done"})
    public void invYourEmailChecking(){
        application.goToArticle3();
        application.goToForwardThisPage();
        for (int i=0;i<10; i++) {
            application.yourEmailValidator(data.InvEmailData(i));
            hb.waitForElementVisible(By.xpath(forward.invalidAlert()));
            assertTrue(hb.isElementPresent(By.xpath(forward.invalidAlert())));
            application.yourEmailClear();
        }
    }
    // positive "Your email" field test
    @Test (groups = {"done"})
    public void yourEmailChecking(){
        application.goToArticle3();
        application.goToForwardThisPage();
        for (int i=0;i<10; i++) {
            application.yourEmailValidator(data.ValidEmailData(i));
            assertTrue(hb.isElementVisible(By.xpath(forward.invalidAlert())));
            application.yourEmailClear();
        }
    }
    //email with top-level hostname consisted of 5 or more characters
    @Test (groups = {"done"})
    public void messageSendingWithEHN(){
        application.goToArticle3();
        application.goToForwardThisPage();
        application.messageSending(data.InvalidEmailHN());
        assertTrue(hb.isElementPresent(By.xpath(articlePage.alertRolling())));
    }
    //negative "Send to" field test
    @Test (groups = {"done"})
    public void invSendToChecking(){
        application.goToArticle3();
        application.goToForwardThisPage();
        for (int i=0;i<10; i++) {
            application.sendToValidator(data.InvEmailData(i));
            application.alertWait();
            assertTrue(hb.isElementPresent(By.xpath(forward.invalidAlert())));
            application.sendToClear();
        }
    }
    // positive "Send to" field test
    @Test (groups = {"done"})
    public void sendToChecking(){
        application.goToArticle3();
        application.goToForwardThisPage();
        for (int i=0;i<10; i++) {
            application.sendToValidator(data.ValidEmailData(i));
            assertTrue(hb.isElementVisible(By.xpath(forward.invalidAlert())));
            application.sendToClear();
        }
    }
    // message field size test
    @Test (groups = {"done"})
    public void messageSizeChanging(){
        application.goToArticle3();
        application.goToForwardThisPage();
        application.reductionMessageTextAreaSize();
        assertFalse(application.isSizeReduced(hb.findElement(By.xpath(forward.message()))));
    }
}
